package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MyShopCollectActivity;
import com.xindaoapp.happypet.bean.ShopCollect;

public class MyShopCollectAdapter extends XinDaoBaseAdapter<ShopCollect> {

	private boolean isEditMode;

	public MyShopCollectAdapter(Context context, List<ShopCollect> iniData, int pageSize, int res,
			int loadingRes) {
		super(context, iniData, pageSize, res, loadingRes);
	}

	@Override
	public void nextPage(int start, int size,
			com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<ShopCollect> iLoadNextPageData) {

		MyShopCollectActivity.getData(mContext, start, size, iLoadNextPageData);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent, final ShopCollect info) {
		ViewHolder holder = (ViewHolder) convertView.getTag();
		if (null == holder) {
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.renqi = (TextView) convertView.findViewById(R.id.renqi);
//			holder.check_box = (ImageView) convertView.findViewById(R.id.check_box);
			convertView.setTag(holder);
		}
		
//		if(isEditMode){
//			holder.check_box.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					info.isChecked = !info.isChecked;
//					notifyDataSetChanged();
//				}
//			});
//			holder.check_box.setVisibility(View.VISIBLE);
//			if(info.isChecked){
//				holder.check_box.setSelected(false);
//			}else{
//				holder.check_box.setSelected(true);
//			}
//		}else{
//			holder.check_box.setVisibility(View.GONE);
//		}
		ImageLoader.getInstance().displayImage(info.goods_thumb, holder.icon);
		holder.title.setText(info.goods_name);

		holder.price.setText("￥" + info.shop_price);
		holder.price.setText(changeTextColor(holder.price, 1, info.shop_price.length()));

		holder.renqi.setText("人气:" + info.popularity);
		holder.renqi.setText(changeTextColor(holder.renqi, 3, info.popularity.length()));

		return convertView;
	}

	private class ViewHolder {
		ImageView icon;
		TextView title;
		TextView price;
		TextView renqi;
//		ImageView check_box;
	}

	private SpannableStringBuilder changeTextColor(TextView tv, int start, int end) {
		SpannableStringBuilder builder = new SpannableStringBuilder(tv.getText().toString());
		ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor("#DA583C"));
		builder.setSpan(redSpan, start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return builder;
	}

//	public void setEditMode(boolean isEditMode) {
//		this.isEditMode = isEditMode;
//		
//	}

}
